package me.firstapp.spi.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.system.JsonEmailConfig;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.system.EmailConfigService;

@Service
public class EmailConfigServiceImpl extends BaseServiceImpl implements EmailConfigService {

	@Override
	public SingleObject<JsonEmailConfig> addEmailConfig(String apiUrl, String apiUser, String apiKey, String mailFrom) {
		SingleObject<JsonEmailConfig> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("apiUrl", apiUrl);
			params.put("mailFrom", mailFrom);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("apiUser", apiUser);
			parameters.put("key", apiKey);

			JSONObject jsonObject = apiBody(parameters, "/emailConfig/addEmailConfig.do", "新增邮件配置", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonEmailConfig config = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonEmailConfig.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(config);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonEmailConfig> updateEmailConfig(Long id, String apiUrl, String apiUser, String apiKey,
			String mailFrom, Integer status) {
		SingleObject<JsonEmailConfig> result = new SingleObject<>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("apiUrl", apiUrl);
			params.put("mailFrom", mailFrom);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);
			parameters.put("status", status);
			parameters.put("apiUser", apiUser);
			parameters.put("key", apiKey);

			JSONObject jsonObject = apiBody(parameters, "/emailConfig/updateEmailConfig.do", "更新邮件配置", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonEmailConfig config = JSONObject.toJavaObject(jsonObject.getJSONObject("object"),
						JsonEmailConfig.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(config);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonEmailConfig> findEmailConfigPage(Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonEmailConfig> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/emailConfig/findEmailConfigPage.do", "分页查询邮件配置",
					searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonEmailConfig> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonEmailConfig> configList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonEmailConfig config = JSONObject.toJavaObject(_jsonObject, JsonEmailConfig.class);
								configList.add(config);
							}
						}
					}
					jsonPage.setList(configList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteEmailConfigs(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("ids", ids);

			JSONObject resultJson = api(parameters, "/emailConfig/deleteEmailConfigs.do", HttpMethod.POST, "删除邮件配置");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonEmailConfig> findEmailConfigById(Long id) {
		SingleObject<JsonEmailConfig> result = new SingleObject<JsonEmailConfig>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("id", id);

			JSONObject resultJson = api(parameters, "/emailConfig/findEmailConfigById.do", HttpMethod.GET, "通過id查詢邮件配置");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonEmailConfig config = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonEmailConfig.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(config);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
